package com.ikdong.toodle.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.ikdong.toodle.R;
import com.ikdong.toodle.util.FolderManager;
import com.ikdong.toodle.widget.FolderListAdapter;

public class FolderListActivity extends BasicActivity implements ActionBar.OnNavigationListener{
	private ListView listView;
	private FolderListAdapter adapter;
	private FolderManager manager;
	private int position = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.folder_list);        
        manager = new FolderManager(this);        
        initView();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		getSupportActionBar().setTitle("");
		
        MenuItem addItem = menu.add("Add Task");
        addItem.setIcon(R.drawable.ic_action_add);
        addItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        addItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent(FolderListActivity.this,FolderAddActivity.class);
				intent.putExtra(FolderAddActivity.OPERATION, FolderAddActivity.OP_ADD);
				startActivity(intent);
				return true;
			}});

        return true;
    }
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    MenuInflater inflater = this.getMenuInflater();
	    inflater.inflate(R.menu.task_list_menu, menu);
	    
	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
	    menu.setHeaderTitle(adapter.getItem(info.position).getName());
	}
	
	@Override
	public boolean onContextItemSelected(android.view.MenuItem item) {
		final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    switch (item.getItemId()) {
	        case R.id.tli_menu_delete:
	        	new AlertDialog.Builder(FolderListActivity.this)
					.setTitle(getString(R.string.msg_alert_folder_delete_title))
					.setMessage(getString(R.string.msg_alert_folder_delete_body))
					.setPositiveButton(getString(R.string.lable_action_ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int which) {
									boolean flag = manager.deleteByStatus(info.id);
									if (flag) {
										adapter.removeItem(info.id);
										adapter.notifyDataSetChanged();
									}
								}
							})
					.setNegativeButton(getString(R.string.lable_action_cancel),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int which) {
								}
							})
					.show();
	            return true;
	        case R.id.tli_menu_edit:
	        	Intent intent = new Intent(this,FolderAddActivity.class);
				intent.putExtra(FolderAddActivity.OPERATION, FolderAddActivity.OP_UPDATE);
				intent.putExtra(FolderAddActivity.FID, info.id);
				startActivity(intent);
	            return true;
	        default:
	            return true;
	    }
	}
	
	private void initView(){
		Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.folder_views, R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(list, this);  
		
		
		listView = (ListView) this.findViewById(R.id.fl_list);
		adapter = new FolderListAdapter(this);
		listView.setTextFilterEnabled(true);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id){
				Intent intent = new Intent(FolderListActivity.this,FolderAddActivity.class);
				intent.putExtra(FolderAddActivity.OPERATION, FolderAddActivity.OP_UPDATE);
				intent.putExtra(FolderAddActivity.FID, listView.getAdapter().getItemId(position));
				startActivity(intent);
			}			
		});
		registerForContextMenu(listView);
	}
	
	private void initData(int position){	
		if(position==0){
			adapter.setFolders(manager.getUnArchived());
		}else{
			adapter.setFolders(manager.get());
		}
		
		if(adapter.getCount()>0){
			listView.setVisibility(View.VISIBLE);
		}else{
			listView.setVisibility(View.GONE);
		}
		
		adapter.notifyDataSetChanged();
	}

	@Override
	void refresh(){
		initData(position);
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        initData(itemPosition);
		return true;
	}
}
